import {Injectable} from '@angular/core';
import {ExecActionInterface} from "./exec-action-interface";
import {ParamDef} from "../model/param-def";
import {ParamDefType} from "../model/param-def-type";
import {MatchExecActionService} from "./match-exec-action.service";
import {FileInfo} from "../model/file-info";
import DateTime from 'xdatetime';
import {SystemUtilsService} from "../service/system-utils.service";

@Injectable({
  providedIn: 'root'
})
export class InfoExtractExecActionService  extends ExecActionInterface{
  private readonly extractItem:ParamDef;
  private readonly extractMethod:ParamDef;
  private readonly extractValue:ParamDef;

  constructor(match:MatchExecActionService,protected system:SystemUtilsService) {
    super("信息提取", "extract");
    this.extractItem = new ParamDef(ParamDefType.Select, "extractItem")
    this.extractMethod =new ParamDef(ParamDefType.Select, "extractMethod")
    this.extractValue =new ParamDef(ParamDefType.Text, "extractValue")
    this.paramsDef=[
      this.extractItem,this.extractMethod,this.extractValue
    ]
    this.extractItem.values = match.matchItem.values
    this.extractMethod.addNameValue("索引提取","strLimit","请输入提取位数(索引从1开始):起始索引,长度")
    this.extractMethod.addNameValue("分隔符提取","strSplit","请输入分隔符以及提取位数(索引从1开始):分隔符,第几个索引")
    this.extractMethod.addNameValue("正则提取","strRegex","请输入提取正则表达式")
    this.extractMethod.addNameValue("时间提取-年","dateYear","请输入默认值")
    this.extractMethod.addNameValue("时间提取-月","dateMonth","请输入默认值")
    this.extractMethod.addNameValue("时间提取-日","dateDay","请输入默认值")
    this.extractMethod.addNameValue("时间提取-小时","dateHour","请输入默认值")
    this.extractMethod.addNameValue("时间提取-分钟","dateMinute","请输入默认值")

    // this.extractMethod.addNameValue("GPS信息-提取国家","gpsCountry")
    // this.extractMethod.addNameValue("GPS信息-提取省","gpsProvince")
    // this.extractMethod.addNameValue("GPS信息-提取市","gpsCity")
    // this.extractMethod.addNameValue("GPS信息-提取区","gpsDistrict")
    this.extractMethod.addNameValue("脚本提取","script")
  }

  async exec(fileInfo: FileInfo, lastVal: any, param: any): Promise<boolean> {
    let extractItemValue = eval("fileInfo."+param.extractItem)
    let method = param.extractMethod
    if (method.startsWith("date")){
      return  this.processDate(method,extractItemValue,param.extractValue)
    }else if (method.startsWith("str")){
      return this.processStr(method,extractItemValue,param.extractValue)
    }else if (method.startsWith("gps")){
      return this.processGps(method,extractItemValue,param.extractValue)
    }else if (method == 'script'){
      return eval(param.extractValue)
    }else{
      this.system.info(`无法识别提取指令:${method}`)
    }
    return false;
  }
  public processDate(method:string,extractItemValue:any,extractValue?:any):any{
    let dateVal:DateTime = new DateTime(extractItemValue)
    if (dateVal == null){
      return extractValue;
    }
    switch (method) {
      case "dateYear":
        return dateVal.getYears()
      case "dateMonth":
        return dateVal.getMonths()
      case "dateDay":
        return dateVal.getDays()
      case "dateHour":
        return dateVal.getHours()
      case "dateMinute":
        return dateVal.getMinutes()
    }
    return dateVal.toString("yyyyMMdd")
  }

  public processStr(method:string,extractItemValue:any,extractValue?:any):any{
    switch (method) {
      case "strRegex":
        let arr = new RegExp(extractValue).exec(extractItemValue)
        if (arr){
          return arr[0];
        }
        break;
      case "strSplit":
        if (extractValue && typeof extractValue == "string"){
          let sp= extractValue.split(",")
          if (sp.length == 2){
              let vsp =extractItemValue.split(sp[0])
              if (vsp.length >= Number(sp[1])){
                return vsp[Number(sp[1])-1]
              }else{
                this.system.info(`[分隔符提取]提取失败:索引超出,最大索引= ${vsp.length}`)
                return false
              }
          }
        }
        this.system.info(`[分隔符提取]输入格式错误:${extractValue}`)
        return false
      case "strLimit":
        if (extractValue && typeof extractValue == "string"){
          let sp = extractValue.split(",")
          if (sp.length == 2){
            if (Number(sp[0]) > extractItemValue.length ){
              this.system.info(`[索引提取]索引越界:最大索引 = ${extractItemValue.length}`)
              return false
            }
            return extractItemValue.substr(Number(sp[0])-1,Number(sp[1]))
          }
        }
        this.system.info(`[索引提取]输入格式错误:${extractValue}`)
        return false

    }
    return false
  }
  public processGps(method:string,extractItemValue:any,extractValue?:any):any{

  }
}
